#include <iostream>
#include <unistd.h>

#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>
#include <libpmemobj++/container/string.hpp>

struct root {
	pmem::obj::string test_a;
};
pmem::obj::persistent_ptr<root> r;
void example_with_ptr(pmem::obj::pool_base &pop) {
	
	pmem::obj::transaction::run(pop, [&] {
		r->test_a = "test_a_string";
	});
	std::cout << r->test_a.c_str() << std::endl;
}

static int file_exists(char const *file) {
    return access(file, F_OK);
}
int main() {
	pmem::obj::pool<root> pop;
    const char * path = "dst_file";
    if (file_exists(path) != 0) {
        pop = pmem::obj::pool<root>::create(path, "string_example", PMEMOBJ_MIN_POOL);
    } else {
        pop = pmem::obj::pool<root>::open(path, "string_example");
    }
    r = pop.root();

	example_with_ptr(pop);
	pop.close();

	return 0;
}

// g++ -I/usr/local/include pmemobjcpp_string.cpp -lpmemobj -lpthread 